import React from 'react'
import {Modal, Form, Input, Tree, message} from 'antd';
import axios from "axios";
import api from "../../../../api/api";

const {TreeNode} = Tree;

export default class MenuTree extends React.Component {
    constructor(props) {
        super(props)
        this.state = {treeData: [], menuTreeCheckedList: []};
    }


    componentDidMount() {
        console.log("MenuTree初始化==", this.props);
        this.props.onRef(this);
        this.getMenuTree();
    }


    getMenuTree(treeMenuArray) {
        let that = this;
        axios.post(api.api_URL + "/admin/directory/queryDirectoryTreeByOrgCode?orgCode=" + localStorage.getItem('orgCode'), {}, {
            headers: {
                "Authorization": localStorage.getItem('token'),
                "Content-Type": api.ContentType
            }
        }).then(function (res) {
            console.log(res.data)
            if (res.data.code != 1) {
                message.error({content:"获取权限信息失败，请重新尝试！"})
                return;
            }
            let dataList = [];
            for (let i = 0; i < res.data.data.list.length; i++) {
                let resContent = res.data.data.list[i];
                let content = {};
                content.key = resContent.id;
                content.title = resContent.name;
                let childContents = [];
                for (let j = 0; j < resContent.children.length; j++) {
                    let resChildContent = resContent.children[j];
                    let childContent = {};
                    childContent.key = resChildContent.id;
                    childContent.title = resChildContent.name;
                    childContents[j] = childContent;
                }
                content.children = childContents;
                dataList[i] = content;
            }
            that.setState({
                treeData: dataList
            })
            if (treeMenuArray) {
                that.setState({
                    menuTreeCheckedList: treeMenuArray
                })
            }
        }).catch(function (err) {
            message.error({content:"获取权限信息失败，请重新尝试！"})
            console.log(err);
        });
    }

    onSelect = (selectedKeys, info) => {
        console.log('selected', selectedKeys, info);
    };
    onCheck = (checkedKeys, info) => {
        let that = this;
        console.log('onCheck', checkedKeys, info);

        this.setState({
            menuTreeCheckedList: checkedKeys
        })
    };
    setItemsValue = (treeMenuArray) => {
        this.setState({
            menuTreeCheckedList: treeMenuArray
        })
    }
    getItemsValue = () => {    //3、自定义方法，用来传递数据（需要在父组件中调用获取数据）
        const valus = this.state.menuTreeCheckedList;       //4、getFieldsValue：获取一组输入控件的值，如不传入参数，则获取全部组件的值
        return valus;
    }

    render() {
        return (
            <Tree checkable
                  onSelect={this.onSelect}
                  onCheck={this.onCheck}
                  defaultExpandParent={true}
                  checkedKeys={this.state.menuTreeCheckedList}
                  treeData={this.state.treeData}
                  disabled={false}
            >
            </Tree>
        );
    }
}
